package com.dnjn.happiness.emo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.InnerAuth;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.domain.UserHcmQxPtdata;
import com.dnjn.happiness.emo.domain.EmoUserRecordPos;
import com.dnjn.happiness.emo.domain.vo.EmoUserRecordPosVO;
import com.dnjn.happiness.emo.service.IEmoUserRecordPosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 消费记录Controller
 *
 * @author happiness
 * @date 2024-03-13
 */
@Tag(name = "emo pos 消费记录", description = "消费记录")
@RestController
@RequestMapping("/pos")
public class EmoUserRecordPosController extends BaseController {
    @Autowired
    private IEmoUserRecordPosService emoUserRecordPosService;


    /**
     * 查询消费记录列表
     */
    //@RequiresPermissions("@ss.hasPermi('emo:pos:list')")
    @PostMapping("/list")
    @Log(title = "emo", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody EmoUserRecordPosVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        EmoUserRecordPos emoUserRecordPos = new EmoUserRecordPos();
        BeanUtils.copyPropsIgnoreNull(vo, emoUserRecordPos);
        Page page = Page.of(current, size);
        Page<EmoUserRecordPos> list = emoUserRecordPosService.page(page, Wrappers.query(emoUserRecordPos));
        return success(list);
    }

    /**
     * 获取消费记录详细信息
     */
    //@RequiresPermissions("@ss.hasPermi('emo:pos:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(emoUserRecordPosService.getById(id));
    }

    /**
     * 新增消费记录
     */
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    //@RequiresPermissions("@ss.hasPermi('emo:pos:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EmoUserRecordPosVO vo) {
        EmoUserRecordPos emoUserRecordPos = new EmoUserRecordPos();
        BeanUtils.copyPropsIgnoreNull(vo, emoUserRecordPos);

        emoUserRecordPos.setCreateBy(SecurityUtils.getUsername());
        return toAjax(emoUserRecordPosService.save(emoUserRecordPos));
    }

    /**
     * 修改消费记录
     */
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("@ss.hasPermi('emo:pos:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody EmoUserRecordPosVO vo) {
        EmoUserRecordPos emoUserRecordPos = new EmoUserRecordPos();
        BeanUtils.copyPropsIgnoreNull(vo, emoUserRecordPos);
        return toAjax(emoUserRecordPosService.updateById(emoUserRecordPos));
    }

    /**
     * 删除消费记录
     */
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
    //@RequiresPermissions("@ss.hasPermi('emo:pos:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(emoUserRecordPosService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 根据日期区间落库消费记录
     */
    @Operation(description = "根据日期区间落库消费记录")
    @PostMapping("/queryPosRecordByTime")
    public AjaxResult queryPosRecordByTime(@Parameter(description = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = true) String startTime,
                                           @Parameter(description = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = true) String endTime) {
        return emoUserRecordPosService.queryPosRecordByTime(startTime, endTime);
    }

    /**
     * 查询昨天得消费记录
     */
    @Operation(description = "查询昨天得消费记录")
    @PostMapping("/getYesterdayPosRecord")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getYesterdayPosRecord() {

        return emoUserRecordPosService.insertYesterdayPosRecord();
    }


}
